<?php
/**
 * @authors liuqinglin 
 * @email (2961181429@qq.com) 
 * @(https://my.oschina.net/shp32131)
 * @date  2016-12-02 19:41:17
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Config_model extends CI_Model {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    //插入一个配置项的数据
    public function insert($data)
    {
    	if($this->db->table_exists('config'))
        {
            if($this->db->insert('config',$data))
            {
                return TRUE;
            }
        }
        return FALSE;
    }
    //获取配置表的所有数据
    public function get_all()
    {
        if($this->db->table_exists('config'))
        {
            $this->db->order_by('id','DESC');
            return $this->db->get('config')->result_array();
        }
        return FALSE;
    }
    //获取匹配条件的一条记录
    public function get_one($cloumn,$value)
    {
        if($this->db->table_exists('config'))
        {
            $mytable = $this->db->dbprefix('config');
            $result = $this->db->query("SELECT * FROM $mytable WHERE $cloumn=$value LIMIT 1");
            return $result->row_array();
        }
        return FALSE;   
    }
    //删除一条记录
    public function delete_one($cloumn,$value)
    {
        if($this->db->table_exists('config'))
        {
            $mytable = $this->db->dbprefix('config');
            if($this->db->query("DELETE FROM $mytable WHERE $cloumn=$value"))
            {
                return TRUE; 
            }
        }
        return FALSE;
    }
    //修改一条记录
    public function update_one($data,$field)
    {
        if(!isset($data) OR !isset($field))
        {
            return FALSE;
        }
        if($this->db->table_exists('config'))
        {
            $mytable = $this->db->dbprefix('config');

            $where = $field['name']."=".$field['value'];
            
            $sql = $this->db->update_string($mytable,$data,$where);

            if($this->db->query($sql))
            {
                return TRUE;
            }
        }
        return FALSE;       
    }  
}